package com.yx.elastic.elasticcm.base;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;

/**
 * Created by chenzp on 18-8-1.
 */
public interface ISearch<T> {

    /**
     * 创建节点
     * @param settings
     * @param mappings
     * @return
     * @throws ISearchException
     */
    boolean createIndex(JSONObject settings,JSONObject mappings) throws ISearchException;

    /**
     * 判断是否存在这个节点名称
     * @param index 节点名称
     * @return
     */
    boolean hasIndex(String index) throws ISearchException;

    /**
     * 判断该节点是否存在
     * @return
     */
    boolean hasIndex() throws ISearchException;

    /**
     * 删除节点
     * 先判断节点是否存在，存在删除
     * @return
     * @throws ISearchException
     */
    boolean deleteIndex() throws ISearchException;

    /**
     * 删除节点
     * 先判断节点是否存在，存在删除
     * @param index 节点名称
     * @return
     * @throws ISearchException
     */
    boolean deleteIndex(String index) throws ISearchException;

    /**
     * 查看节点api信息
     * @return
     */
    JSONObject getApi() throws ISearchException;


    /**
     * 查看节点信息
     * @param index 节点名称
     * @return
     */
    JSONObject getApi(String index) throws ISearchException;

    /**
     *
     * 更新与写入操作
     * 写入文档数据,如果存在该文档做更新操作
     * @param object 要操作的elastic orm对象
     */
    IndexResponse index(T object) throws ISearchException;

    /**
     * 更新与写入操作
     * 写入文档数据,如果存在该文档做更新操作
     * @param list 要操作的elastic orm集合对象
     * @return
     */
    BulkResponse indexBatch(List<T> list) throws ISearchException;

    /**更新文档数据
     * 针对注解对象处理
     * @param object 要操作的elastic orm对象
     * @return
     */
    UpdateResponse update(T object) throws ISearchException;

    /**
     * 批量更新
     * 针对注解对象处理
     * @param list 要操作的elastic orm对象集合
     * @return
     */
    BulkResponse updateBatch(List<T> list) throws ISearchException;

    /**
     * 根据对象获取文档
     * @param object
     * @return
     */
    T get(T object) throws ISearchException;


    /**
     * 根据id获取文档
     * @param id
     * @return
     */
    T getById(Object id) throws ISearchException;

    /**
     * 删除文档
     * @param object
     */
    DeleteResponse delete(T object) throws ISearchException;

    /**
     * 删除文档
     *
     * @param index 索引
     * @param type 文档类型
     * @param id 文档id
     * @return
     */
    DeleteResponse delete(String index,String type,String id) throws ISearchException;



    /**
     * 删除文档
     *
     * @param id 文档id
     * @return
     */
    DeleteResponse deleteById(Object id) throws ISearchException;

    /**
     * 批量删除文档
     * @param list
     */
    BulkResponse deleteBatch(List<T> list) throws ISearchException;

    /**
     * 根据id批量删除
     * @param index
     * @param type
     * @param ids
     */
    BulkResponse deleteBatchById(String index,String type,List<Object> ids) throws ISearchException;

    /**
     * 根据id集合删除
     * @param ids
     * @return
     */
    BulkResponse deleteBatchById(List<Object> ids) throws ISearchException;

    /**
     *
     * @param boolQueryBuilder
     * @return
     */
    SearchResponse query(BoolQueryBuilder boolQueryBuilder) throws ISearchException;



    SearchResponse query(SearchSourceBuilder searchSourceBuilder) throws ISearchException;

    SearchResponse query(SearchRequest searchRequest) throws ISearchException;



}
